<?php

require_once ('common.php');
$action = trim($_REQUEST['action']);
$topic_id = trim($_REQUEST['topic_id']);
if (isset($action))
    checkLogin();
if ($action == 'add')
{
    if (empty($topic_id))
    {
        header('location: ' . APP_URL);
        exit;
    }
    $post_poster = $uid;
    $post_text = trim(stripslashes($_POST['post_text']));
    $post_time = getTime();
    addPost($topic_id, $post_poster, $post_text, $post_time);
    header('location: ' . APP_URL . '/topic/' . $topic_id);
    exit;
} elseif ($action == 'edit')
{
    $post_id = trim($_REQUEST['id']);
    if (empty($post_id))
    {
        header('location: ' . APP_URL);
        exit;
    }
    $topic = getPost($post_id);
    $post_editor = $uid;
    $post_text = trim(stripslashes(htmlspecialchars($_POST['post_text'])));
    $edit_time = getTime();
    editPost($post_id, $post_editor, $post_text, $edit_time);
    header('location: ' . APP_URL . '/topic/' . $topic['topic_id']);
    exit;
} elseif ($action == 'delete')
{
    $post_id = trim($_REQUEST['id']);
    if (empty($post_id))
    {
        header('location: ' . APP_URL);
        exit;
    }
    $post = getPost($post_id);
    $post_deletor = $uid;
    deletePost($post_id, $post_deletor);
    header('location: ' . APP_URL . '/topic/' . $post['topic_id']);
    exit;
}
else
    header('location: ' . APP_URL);

?>